package com.hmall.api.config;

import com.hmall.api.client.fallback.*;
import com.hmall.common.utils.UserContext;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;

/**
 * 定义Feign的日志级别
 */
public class DefaultFeignConfig {

    /**
     * 定义日志级别
     * @return
     */
    @Bean
   public Logger.Level defaultFeignLog(){
        return Logger.Level.FULL;
    }

    /**
     * 各微服务之间基于OpenFeign提供的拦截器RequestInterceptor进行信息的传递
     * @return
     */
    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                Long userId = UserContext.getUser();
                if (userId != null){
                    //设置请求头，传递信息
                    requestTemplate.header("UserInfo",userId.toString());
                }
            }
        };
    }

    /**
     * 将ItemClientFallbackFactory注册成为一个bean类
     * @return
     */
    @Bean
    public ItemClientFallbackFactory itemClientFallbackFactory(){
        return new ItemClientFallbackFactory();
    }

    /**
     * 将UserClientFallbackFactory注册成为一个bean类
     * @return
     */
    @Bean
    public UserClientFallbackFactory userClientFallbackFactory(){
        return new UserClientFallbackFactory();
    }

    /**
     * 将TradeClientFallbackFactory注册成为一个bean类
     * @return
     */
    @Bean
    public TradeClientFallbackFactory tradeClientFallbackFactory(){
        return new TradeClientFallbackFactory();
    }

    /**
     * 将CartClientFallbackFactory注册成为一个bean类
     * @return
     */
    @Bean
    public CartClientFallbackFactory cartClientFallbackFactory(){
        return new CartClientFallbackFactory();
    }

    /**
     * 将PayClientFallbackFactory注册成为一个bean类
     * @return
     */
    @Bean
    public PayClientFallbackFactory payClientFallbackFactory(){
        return new PayClientFallbackFactory();
    }
}
